package me.boot.web.common.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import me.boot.db.mybatis.template.SuperService;
import me.boot.db.mybatis.template.entity.Entity;
import me.boot.web.common.template.model.PageData;
import me.boot.web.common.template.model.PageParam;
import me.boot.web.common.template.model.QueryParam;
import me.boot.web.common.template.model.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;

public interface SelectController<
        ID extends Serializable,
        PO extends Entity<ID>,
        S extends SuperService<ID, PO>,
        DTO>
        extends PageController<ID, PO, S, DTO> {


    @ApiOperation(value = "单体查询")
    @ApiOperationSupport(order = 41)
    @GetMapping(value = "/select/{id}")
    default Result<PO> getById(@PathVariable ID id) {
        return Result.newSuccess(getService().getById(id));
    }

    @ApiOperation(value = "列表查询")
    @ApiOperationSupport(order = 42)
    @PostMapping(value = "/select/list")
    default Result<List<PO>> getList(@RequestBody @Validated QueryParam queryParam) {
        QueryWrapper<PO> wrapper = queryParam.wrapper(getPoClass());
        processListQueryWrapper(wrapper);
        return Result.newSuccess(getService().list(wrapper));
    }

    @ApiOperation(value = "分页查询")
    @ApiOperationSupport(order = 43)
    @PostMapping(value = "/select/page")
    default Result<PageData<PO>> getPage(@RequestBody @Validated PageParam<DTO> pageParam) {
        IPage<PO> page = pageQuery(pageParam);
        PageData<PO> pageData = new PageData<>(
                (int) page.getTotal(),
                (int) page.getPages(),
                (int) page.getCurrent(),
                (int) page.getSize(),
                page.getRecords());

        return Result.newSuccess(pageData);
    }

    default void processListQueryWrapper(QueryWrapper<PO> wrapper) {
    }

}
